const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            policy: [],
            city:[],
            city1:[],
            land: [],
            record: [],
            blue_plate:[],
            rules: {
                Party_Organization_Name: [
                    { required: true, message: "党组织名称不能为空", trigger: "blur" },
                    { min: 2, max: 50, message: "长度在2-50个字符", trigger: "blur" },
                ],
                Establishment_Date: [
                    { required: true, message: "请选择成立日期", trigger: "change" },
                ],
                Party_Organization_Type: [
                    { required: true, message: "请选择组织类型", trigger: "change" },
                ],
                Party_Organization_Level: [
                    { required: true, message: "请选择党组级别", trigger: "change" },
                ],
                Party_Organization_Address: [
                    { required: true, message: "请输入党组地址", trigger: "blur" },
                    { min: 5, max: 100, message: "长度在5-100个字符", trigger: "blur" },
                ],
                Party_Organization_Captain_Number: [
                    { required: true, message: "请输入负责人编号", trigger: "blur" },
                ],
            },
            value: [], // 初始化 labelNumber 属性
            hjoptions: [], //省市区县
            Nation: [], //民族
            Type: [
                {
                    value: "党委",
                    label: "党委",
                },
                {
                    value: "总支",
                    label: "总支",
                },
                {
                    value: "支部",
                    label: "支部",
                },
            ],
            Manager: [], //上级党组
            Level: [
                //党组级别
                {
                    value: "省",
                    label: "省",
                },
                {
                    value: "市",
                    label: "市",
                },
                {
                    value: "区/县",
                    label: "区/县",
                },
                {
                    value: "乡镇/街道",
                    label: "乡镇/街道",
                },
                {
                    value: "村",
                    label: "村",
                },
            ],
            Enterprise: [], //党组所在企业
            Manager1: [],
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        this.nodeKey = getQueryObject(ls_URL, "nodeKey");// 获取 nodeKey 参数
        this.level = getQueryObject(ls_URL, "level");// 获取 nodeKey 参数
        this.dataForm.Parent_SerialNumber = this.nodeKey;


        this.getCategory();
    },
    watch: {
        'dataForm.Policy_SerialNumber'(val) {
            if (val) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                    type: "POST",
                    data: {
                        SQLSerialNumber: "f_subsidy_policy_detail",
                        Condition: window.btoa(unescape(encodeURIComponent(val))),
                    },
                    success: (data) => {
                        this.record = data;
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            }
        }
    },
    methods: {
        changeParent(arr) {
            let val = arr[arr.length - 1]
            let obj = this.Manager1.filter(i => i.Party_Organization_SerialNumber == val)[0];
            if (obj) {
                this.dataForm.Level = parseInt(obj.Level) + 1
            }
        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'party_organization',
                    NodeCode: ''
                },
                success: (data) => {
                    data.forEach(item => {
                        if (item.children && item.children.length > 0) {
                            item.children.forEach(item => {
                                if (item.children && item.children.length > 0) {
                                    item.children.forEach(ite => {
                                        ite.children = undefined
                                    })
                                }
                            })
                        }
                    })
                    console.log(data);
                    this.Manager = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'party_organization',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    console.log(data);
                    this.Manager1 = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'subsidy_policy',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.policy = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'government_agencies',
                    Condition: `Row_Status='0'`,
                },
                success: (res) => {
                    this.Enterprise = res.filter(i=>!i.Agensies_Name.includes('办公室'));
                    console.log(this.Enterprise);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/clientOperation/getDistrictTree`,
                type: "GET",
                data: {
                  DistrictCode: '232723',
                },
                success: (data) => {
                    this.city = data;
                },
                complete: (XMLHttpRequest) => {
                  requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                  SQLSerialNumber: 'f_blue_plate_information1',
                  Condition: '',
                  ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND blue_plate_information.Plate_Type_SerialNumber='党支部'"))),
                },
                success: (res) => {
                  this.blue_plate = res;
                  console.log(res);
                },
                complete: (XMLHttpRequest) => {
                  requestPretreatment(XMLHttpRequest);
                },
            });
        },
        handleChange(val) {
            if (val) {
                this.dataForm.Province_Number = val.substring(0,2) + '0000'
                this.dataForm.City_Number = val.substring(0,4) + '00'
                this.dataForm.County_Nubmer = val.substring(0,6);
                this.dataForm.Town_Number = val.substring(0,9);
            }
            
            
        },
        changeBluePlate(val) {
            let obj = this.blue_plate.filter(i => i.Blue_Plate_SerialNumber == val)[0];
            if (obj) {
                this.dataForm.Village_Number = obj.Village_SerialNumber
                this.handleChange(obj.Village_SerialNumber);
                this.dataForm.Party_Organization_Address = obj.Address;
                this.dataForm.Coord = obj.Coord;
            }
        },
        handleEnterprise(val) {
            console.log(val);
        },
        // changeRecord(val){
        //     $.ajax({
        //         url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        //         type: "POST",
        //         data: {
        //             SQLSerialNumber: "f_subsidy_policy_detail",
        //             Condition: window.btoa(unescape(encodeURIComponent(val))),
        //         },
        //         success: (data) => {
        //             this.record = data;
        //         },
        //         dataType: "json",
        //     });
        // },
        changeRecord(val) {
            if (val) {
                this.dataForm.Land_SerialNumber = this.record.filter(i => i.Record_SerialNumber == val)[0].Land_SerialNumber
            } else {
                this.dataForm.Land_SerialNumber = '';
            }
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                        let lJSONObject_ChildData_Options = new Object();
                        lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                        let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                        let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                        // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                        //建立子表结构体
                        let JSONArray_ChildRecord = new Array();
                        JSONArray_ChildRecord = this[ls_ChildDataForm];
                        if (!this[ls_DataForm].Children) {
                            this[ls_DataForm].Children = new Object();
                        }
                        this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                    })
                } catch (error) {

                }
                if (this.level != '' && !this.dataForm.Level) {
                    this.dataForm.Level = parseInt(this.level) + 1
                }
                //建立新增提交结构
                let lJSONObject_DataSet = new Object();
                if (action == 'Add') {
                    if (this.nodeKey) {
                        this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                    }
                    this[ls_DataForm].News_Publish_Datetime = getYMDHMSDate();
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(this[ls_DataForm]);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                    lJSONObject_DataSet[action] = lJSONObject_Table;
                } else if (action == 'Modify') {
                    const { add, modify } = this.deal(this[ls_DataForm]);
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(modify);

                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    lJSONObject_DataSet['Add'] = new Object();
                    if (add != null) {
                        let lJSONArray_Record_Add = new Array();
                        lJSONArray_Record_Add.push(add);

                        let lJSONObject_Table_Add = new Object();
                        lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                        lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                    }
                    lJSONObject_DataSet['Add']['party_organization_log'] = [{
                        Party_Organization_SerialNumber: this.dataForm.Party_Organization_SerialNumber,
                        Party_Organization_Name: this.dataForm.Party_Organization_Name_Original,
                        New_Party_Organization_Name: this.dataForm.Party_Organization_Name,
                        Establishment_Date: this.dataForm.Establishment_Date_Original,
                        New_Establishment_Date: this.dataForm.Establishment_Date,
                        Parent_SerialNumber: this.dataForm.Parent_SerialNumber_Original,
                        New_Parent_SerialNumber: this.dataForm.Parent_SerialNumber,
                        Level: this.dataForm.Level_Original,
                        New_Level: this.dataForm.Level,
                        Party_Organization_Type: this.dataForm.Party_Organization_Type_Original,
                        New_Party_Organization_Type: this.dataForm.Party_Organization_Type,
                        Party_Organization_Level: this.dataForm.Party_Organization_Level_Original,
                        New_Party_Organization_Level: this.dataForm.Party_Organization_Level,
                        Party_Organization_Address: this.dataForm.Party_Organization_Address_Original,
                        New_Party_Organization_Address: this.dataForm.Party_Organization_Address,
                        Party_Organization_Coords: this.dataForm.Party_Organization_Coords_Original,
                        New_Party_Organization_Coords: this.dataForm.Party_Organization_Coords,
                        Party_Organization_Captain_Number: this.dataForm.Party_Organization_Captain_Number_Original,
                        New_Party_Organization_Captain_Number: this.dataForm.Party_Organization_Captain_Number,
                        Enterprise_SeriralNumber: this.dataForm.Enterprise_SeriralNumber_Original,
                        New_Enterprise_SeriralNumber: this.dataForm.Enterprise_SeriralNumber,
                    }]
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet,(res) => {
                    if (res.Status == 'success') {
                        this.$confirm('是否跳转到换届补选前往配置, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            window.location.href = '../../party_organization_tenure/index.html'
                        }).catch(() => {
                            window.location.href = './index.html'
                        });
                    }
                })
                // $.a
                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalSave`,
                //     method: "post",
                //     data: {
                //         Data: JSON.stringify(lJSONObject_DataSet),
                //     },
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             this.$confirm('是否跳转到换届补选前往配置, 是否继续?', '提示', {
                //                 confirmButtonText: '确定',
                //                 cancelButtonText: '取消',
                //                 type: 'warning'
                //             }).then(() => {
                //                 window.location.href = '../../party_organization_tenure/index.html'
                //             }).catch(() => {
                //                 window.location.href = './index.html'
                //             });
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         requestPretreatment(XMLHttpRequest);
                //     },
                // });
            }
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-cascader', ylCascader)
app.component('yl-upload', ylUpload);
app.component('yl-select', ylSelect);
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
